if (window.plus) {
    ready();
} else {
    document.addEventListener('plusready', ready, false);
}

$(function(){
  if(AGENT_CLIENT.WEB){
    ready();
  } 
})

function ready() {
    var user = {}; 
    var vcode = ''; 
    var _thisPage = null;
    var html = [];
    var user = null; 
    var passType = "login"; 
    var _parms = {}; 
    var requestUrl = '';
		var backurl = '';
	
    var editpassPage = function () {
        this.init();
    };

    editpassPage.prototype = {
        init: function () {
            this.initData();
            this.bindEvent();
        },
        initData: function () {
            user = getLocalStorage(STORAGE_MANAGER.user);
            if (user) {
                passType = getUrlParam("type");
                vcode = getUrlParam("vcode");
                backurl = getUrlParam("backurl");
                switch (passType) {
                    case "login":
                        $("#updateType").html("修改登录密码");
                        break;
                    case "pay":
                        $("#updateType").html("修改支付密码");
                        break;
                    default:
                        break;
                }
            } else {
                redirect("login.html");
                return false;
            }
        },
        bindEvent: function () {
            //点击回退
            $("#back").on("click", function () {
                back();
            });

            //点击清除
            $("#clear").on("click", function () {
                $("#nPassword,#confirmNpassword").val("");
            });
            
            $("#nPassword,#confirmNpassword").on("input", function(){
              if("" == $("#nPassword").val() || "" == $("#confirmNpassword").val()){
                $("#confirm").addClass("gray_color");
              }else{
                $("#confirm").removeClass("gray_color");
              }
            });
            
            //点击提交
            $("#confirm").on("click", function () {                
                if ("" == $("#nPassword").val()) {
                    $.toast("请填写新密码");
                    return false;
                }
                if ("" == $("#confirmNpassword").val()) {
                    $.toast("请再次填写新原密码");
                    return false;
                }
                if('pay' == passType){
	                if (!isPasswd($("#nPassword").val())) {
	                    $.toast("密码为6至15位数字字母或下划线");
	                    return false;
	                }
                }
                if ( $("#confirmNpassword").val() != $("#nPassword").val() ) {
                    $.toast("两次输入的密码不一致");
                    return false;
                }
                requestUrl = CONFIG_MANAGER.RequstUrl;
                switch (passType) {
                  case "login":
                      requestUrl += "v1/member/safety/loginpasswd/";
                      break;
                  case "pay":
                      requestUrl += "v1/member/safety/paypasswd/";
                      break;
                  default:
                      break;
                }
                
                var postData = {"id": user.id, "vcode": vcode, "passwd": $("#confirmNpassword").val()};
                $.ajax({
                    type: "PUT",
                    url: requestUrl,
                    async: true,
                    data: postData,
                    headers: {"token": user.token}
                }).done(function (data) {
                    $.alert("修改成功", function () {
                    	if(backurl){
                    		//TODO app重新加载跳转页的数据
                    		var redirectUrl = "../" + backurl.replace("|", "/");
                    		redirect(redirectUrl);
                    	}else{
                    		redirect("account.html");
                    	}
                        return false;
                    });
                }).always(function () {
                }).fail(function (data) {
                    $.toast(data.responseJSON.Message);
                });
            });
        },
    }

    _thisPage = new editpassPage();

}
